<?php
    /**
     * Opens a file and shows it inside a frame
     * 
     * $_GET[f] the base64 encoded value of the file name
     */

    require_once 'lib/file.php';
    require_once 'lib/common.php';

    /**
     * 
     * Returns the file name sent to this
     * script. The filename returned is
     * relative to index.php's directory
     * 
     * @return returns the file name to open
     */
    function getFile()
    {
       return base64_decode($_GET["f"]);
    }


    if (!isset($_GET["f"]))
    {
        showFatalError('Error', "Incomplete request");
        exit;   
    }

    $f = new File(getFile());

    # In case this is a folder or is not a file at all
    # Stop execution
    if (!$f->exists())
    {
       showFatalError('404', "Requested file doesn't exist");
       exit;
    }
    
    // Checking permissions
    if (!isFileViewAllowed($f->getFullFilename()))
    {
        showFatalError('Forbidden', "You don't have premission to access '{$f->getFilename()}' on this server");
        exit;
    }

    initSite('Open file : ' . $f->getFilename(), "Located in " . $f->getPath());
    
    # arguments that will be sent to index.php when
    # 'Back to explorer' is clicked, arguments are added
    # arguments are added so explorer will return to this file's directory
    $args = ($f->getPath() == '') ? "" : "?v={$f->getPath()}";
    
    echo makeMenu(array( array('Back to file explorer', 'index.php' . $args) ));

    ?>
        <br>
        <iframe class="fs" src="<?php echo $f->getFullFilename() ?>"></iframe>
    <?php

    finishSite();
?>
